package provision

import com.hp.hpl.jena.rdf.model.*
import com.hp.hpl.jena.query.*
import com.hp.hpl.jena.graph.*
import com.hp.hpl.jena.ontology.*
import com.talis.rdfwriters.json.*;

class BuildRdfService {

    static transactional = false

    def buildRdfNt() {
		def model= ModelFactory.createDefaultModel()
		def briefDescription= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#briefDescription')
		def technicalDescription= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#technicalDescription')
		def payload= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#payload')
		nextgen.Resource.list().each { r ->
			def subject= model.createResource(r.url)
			if (r.brief) { subject.addProperty(briefDescription, r.brief) }
			if (r.technical) { subject.addProperty(technicalDescription, r.technical) }
			if (r.payload) { subject.addProperty(payload, r.payload) }
		}
		def rdfWriter= model.getWriter('N-TRIPLE')
		def output= new FileOutputStream('web-app/resources/provisions.nt')
		rdfWriter.write(model, output, 'http://www.jpdo.gov/provisions')
		output.close()
    }
	
	def buildRdfJson() {
		def model= ModelFactory.createDefaultModel()
		def briefDescription= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#briefDescription')
		def technicalDescription= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#technicalDescription')
		def payload= ResourceFactory.createProperty('http://www.jpdo.gov/nextgen#payload')
		nextgen.Resource.list().each { r ->
			def subject= model.createResource(r.url)
			if (r.brief) { subject.addProperty(briefDescription, r.brief) }
			if (r.technical) { subject.addProperty(technicalDescription, r.technical) }
			if (r.payload) { subject.addProperty(payload, r.payload) }
		}
		def out= new FileOutputStream('web-app/resources/provisions.json')
		def jsonWriter= new JSONJenaWriter()
		jsonWriter.write(model, out, 'http://www.jpdo.gov/provisions')
		out.close()
	}
}
